/**
 * @filename:VswaveServiceImpl Jan 8, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.core.date.DateTime;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.Vswave;
import com.wonder.app.domain.Vswavest;
import com.wonder.app.domain.Wendu;
import com.wonder.app.mapper.VswaveDao;
import com.wonder.app.mapper.VswavestDao;
import com.wonder.app.service.VswaveService;
import com.wonder.app.request.VswaveRequest;
import com.wonder.app.service.VswavestService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import com.alibaba.fastjson.JSON;

import java.util.*;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**   
 * @Description:TODO(VS波形设置服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class VswaveServiceImpl  extends ServiceImpl<VswaveDao, Vswave> implements VswaveService {

	/**
	 * 验证VS波形设置信息
	 *
	 * @param vswave
	 * @param type
	 * @return String
	 */
	@Autowired
	private VswaveDao vswaveDao;
	@Autowired
	private VswaveService vswaveService;
	@Autowired
	private VswavestDao vswavestDao;
	@Autowired
	private VswavestService vswavestService;

	@Override
	public String validate(Vswave vswave, String type) {


		return "";
	}


	/**
	 * Description:VS波形设置分页返回
	 *
	 * @param request //检索条件
	 * @param page    //分页参数
	 * @return IPage<Vswave>   //分页记录
	 */
	@Override
	public IPage<Vswave> selectList(VswaveRequest request, Page<Vswave> page) {
		LambdaQueryWrapper<Vswave> lambdaQueryWrapper = new LambdaQueryWrapper<>();

		if (null != request.getId()) {
			lambdaQueryWrapper.eq(Vswave::getId, request.getId());
		}

		if (StringUtils.hasText(request.getFnumber())) {
			lambdaQueryWrapper.like(Vswave::getFnumber, request.getFnumber());
		}

		if (StringUtils.hasText(request.getFname())) {
			lambdaQueryWrapper.like(Vswave::getFname, request.getFname());
		}

		if (StringUtils.hasText(request.getFworkorder())) {
			lambdaQueryWrapper.like(Vswave::getFworkorder, request.getFworkorder());
		}

		if (StringUtils.hasText(request.getFcreateuser())) {
			lambdaQueryWrapper.like(Vswave::getFcreateuser, request.getFcreateuser());
		}

		if (request.getFcreatedate() != null) {
			Date[] fcreatedate = request.getFcreatedate();
			if (fcreatedate.length > 0)
				lambdaQueryWrapper.ge(Vswave::getFcreatedate, fcreatedate[0]);
			if (fcreatedate.length == 2)
				lambdaQueryWrapper.le(Vswave::getFcreatedate, fcreatedate[1]);

		}

		if (null != request.getFstartstatus()) {
			lambdaQueryWrapper.eq(Vswave::getFstartstatus, request.getFstartstatus());
		}


		lambdaQueryWrapper.orderByDesc(Vswave::getId);
		Page<Vswave> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
	}

	@Override
	public boolean savevswave(String userName, Map<String, Object> header, List<Map<String, Object>> dataList) {
		//获取当前日期
		Date currentDate = new Date();
		Vswave vswave = new Vswave();
		if (header!=null)
		{
//			vswave.setFnumber((String) header.get(""));
			vswave.setFnumber(VtuzxUtil.getString(header,"fnumber"));
			vswave.setFstatus(0);
			vswave.setFcreatedate(currentDate);
			vswave.setFcreateuser(userName);
			vswave.setFupdatedate(currentDate);
			vswave.setFupdateuser(userName);
			vswave.setFname(VtuzxUtil.getString(header,"fname"));
			vswave.setFnote(VtuzxUtil.getString(header,"fnote"));
			vswave.setFpictureId(VtuzxUtil.getInt(header,"fpicture"));
			vswave.setFstartstatus(VtuzxUtil.getInt(header,"fstartstatus"));
			vswave.setFvcom(VtuzxUtil.getString(header,"fvcom"));
//			vswave.setFworkorder();//工单号取消
		}
		boolean res = vswaveService.save(vswave);
		List<Vswavest> detailList = new ArrayList<>();
		long detailid = vswave.getId();
		double sumCount = 0;
		List<Vswave> data = new ArrayList<>();
		for (Map<String, Object> map : dataList) {
			Vswavest detail = new Vswavest();
//			detail.setDetailid(VtuzxUtil.getLong(map, "detailid"));
			detail.setDetailid(detailid);
			detail.setFdianya(VtuzxUtil.getString(map, "fdianya"));
			detail.setFboxing(VtuzxUtil.getString(map, "fboxing"));
			detail.setFngtype(VtuzxUtil.getInt(map, "fngtype"));
			detail.setFemperature(VtuzxUtil.getDouble(map, "femperature"));
			detail.setFresidtime(VtuzxUtil.getDouble(map, "fresidtime"));
			detail.setFrefreshtime(VtuzxUtil.getDouble(map, "frefreshtime"));
			detail.setFtuopanqty(VtuzxUtil.getLong(map, "ftuopanqty"));
			detail.setId(VtuzxUtil.getLong(map, "id"));
			detailList.add(detail);
		}
//		Long id = Long.parseLong(header.get("id").toString());
//		Vswave vswave = vswaveDao.selectById(id);
//		vswave.setFstatus(0);
//		boolean pickRes = vswaveService.updateById(vswave);
		boolean resdetail = vswavestService.saveOrUpdateBatch(detailList);
//		return res && pickRes;
		return  res && resdetail;
	}
	@Override
	public boolean updatewave(String userName, Map<String, Object> header, List<Map<String, Object>> dataList) {
		//获取当前日期
		Date currentDate = new Date();
		Vswave vswave = new Vswave();
		if (header!=null)
		{
			vswave.setId((VtuzxUtil.getLong(header,"id")));
			vswave.setFnumber(VtuzxUtil.getString(header,"fnumber"));
			vswave.setFstatus(0);
			vswave.setFworkorder((VtuzxUtil.getString(header,"fworkorder")));
			vswave.setFupdatedate(currentDate);
			vswave.setFupdateuser(userName);
			vswave.setFname(VtuzxUtil.getString(header,"fname"));
			vswave.setFnote(VtuzxUtil.getString(header,"fnote"));
			vswave.setFpictureId(VtuzxUtil.getInt(header,"fpicture"));
			vswave.setFstartstatus(VtuzxUtil.getInt(header,"fstartstatus"));
			vswave.setFvcom(VtuzxUtil.getString(header,"fvcom"));
		}
		boolean res = vswaveService.updateById(vswave);
		List<Vswavest> detailList = new ArrayList<>();
		List<Vswave> data = new ArrayList<>();
		for (Map<String, Object> map : dataList) {
			Vswavest detail = new Vswavest();
			detail.setDetailid(VtuzxUtil.getLong(map, "detailid"));
			detail.setFdianya(VtuzxUtil.getString(map, "fdianya"));
			detail.setFboxing(VtuzxUtil.getString(map, "fboxing"));
			detail.setFngtype(VtuzxUtil.getInt(map, "fngtype"));
			detail.setFemperature(VtuzxUtil.getDouble(map, "femperature"));
			detail.setFresidtime(VtuzxUtil.getDouble(map, "fresidtime"));
			detail.setFrefreshtime(VtuzxUtil.getDouble(map, "frefreshtime"));
			detail.setFtuopanqty(VtuzxUtil.getLong(map, "ftuopanqty"));
			detail.setId(VtuzxUtil.getLong(map, "id"));
			detailList.add(detail);
		}
		boolean resdetail = vswavestService.saveOrUpdateBatch(detailList);
		return  res && resdetail;
	}

	/**
	 * 获获取vs明细信息
	 * @param id
	 * @return
	 */
	@Override
	public VtuzxMap getDetail(Long id) {
		VtuzxMap data = new VtuzxMap();
		Vswave vswave = vswaveDao.selectById(id);
		//查询明细
		List<Vswavest> vswavestList = vswavestDao.selectList(new QueryWrapper<Vswavest>().eq("detailid",id));
		data.append("header",vswave)
				.append("dataList",vswavestList);
		return data;
	}

}